package net.inxweb.bluemeerkat.web.simple.controller.glossary.type;

import java.util.List;

import javax.annotation.Resource;

import net.inxweb.bluemeerkat.domain.model.glossary.GlossaryType;
import net.inxweb.bluemeerkat.domain.service.GlossaryService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/glossary/type") 
public class GlossaryTypeController {
	@Resource(name="glossaryServiceImpl")
	private GlossaryService glossaryService;
	
	@RequestMapping(value="/createForm", method=RequestMethod.GET) 
    public ModelAndView requestCreateForm() { 
        return new ModelAndView("/glossary/type/create");
    }  
	
	@RequestMapping(value="/create", method=RequestMethod.POST) 
	public ModelAndView requestCreate(@RequestParam(value="glossaryTypeName") String glossaryTypeName) {  
		GlossaryType glossaryType = new GlossaryType(glossaryTypeName); 
		
		glossaryService.createGlossaryType(glossaryType); 
		return new ModelAndView("redirect:/glossary/type/modifyForm.do")
		.addObject("glossaryTypeId", glossaryType.getId())
		.addObject("message", "creating is successful");
	} 
	
	@RequestMapping(value="/modifyForm", method=RequestMethod.GET) 
    public ModelAndView requestModifyForm(@RequestParam(value="glossaryTypeId") Long glossaryTypeId) { 
		GlossaryType glossaryType = glossaryService.findGlossaryType(glossaryTypeId);  		
        return new ModelAndView("/glossary/type/modify")
        .addObject("glossaryType", glossaryType);
        
    } 
	
	@RequestMapping(value="/modify", method=RequestMethod.POST) 
	public ModelAndView requestModify(
			@RequestParam(value="glossaryTypeId") Long glossaryTypeId, 
			@RequestParam(value="glossaryTypeName") String glossaryTypeName) {  
		
		GlossaryType glossaryType = new GlossaryType(glossaryTypeName); 
		
		glossaryType.setId(glossaryTypeId);		
		glossaryService.modifyGlossaryType(glossaryType);
		
		return new ModelAndView("redirect:/glossary/type/modify.do")
		.addObject("glossaryTypeId", glossaryType.getId())
		.addObject("message", "modifing is successful.");
	} 
	
	@RequestMapping(value="/read", method=RequestMethod.GET)
    public ModelAndView requestRead(@RequestParam(value="glossaryTypeId") Long glossaryTypeId) {
		GlossaryType glossaryType = glossaryService.findGlossaryType(glossaryTypeId);  
        return new ModelAndView("/glossary/type/create").addObject("glossaryType", glossaryType);
    } 
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
    public ModelAndView requestList() {
		List<GlossaryType> glossaryTypes = glossaryService.findAllGlossaryType(); 
        return new ModelAndView("/glossary/type/list").addObject("glossaryTypes", glossaryTypes);
    }
}
